#!/bin/sh
#error and exit = set -e
#set -o errexit
#color
source /etc/init.d/functions
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
plain='\033[0m'

function historyConf(){
	PROMPTCOUNT=`grep "PROMPT_COMMAND" /etc/profile |wc -l`
	if [ $PROMPTCOUNT -eq 0 ];then
    	cat >> /etc/profile << \EOF
export HISTTIMEFORMAT="[%F %T] "
export PROMPT_COMMAND='
command_history="$( history 1 |sed "s/^ *\([0-9]*\) *\[[0-9]*-[0-9]*-[0-9]* *[0-9]*:[0-9]*:[0-9]*] *\(.*\)$/\2   #seq=[\1]/g")"
command_history_v=${command_history}" `whoami`"
command_history_vv=${command_history}" $( who am i | sed -r "s|^([^ \t]+)[ \t]+([^ \t]+)[ \t]+(.+)[ \t]+(\(.+\))|user=[\1] tty=[\2] date=[\3] ip=\4|") pwd=[`pwd`]"
echo "$command_history_vv" >> /var/log/usercmd.log
'
EOF
		touch /var/log/usercmd.log
		chmod 622 /var/log/usercmd.log
	else
		echo -e "${yellow}Warn:${plain} history have been configured !"
	fi

}

function chronyConf(){
  sed -i '/^pool /s@pool@#pool@g' /etc/chrony.conf
  sed -i '/^#pool /a\pool ntp3.aliyun.com iburst' /etc/chrony.conf
  sed -i '/^#pool /a\pool ntp2.aliyun.com iburst' /etc/chrony.conf
  sed -i '/^#pool /a\pool ntp1.aliyun.com iburst' /etc/chrony.conf
  sed -i '/^#pool /a\pool ntp0.aliyun.com iburst' /etc/chrony.conf

  systemctl enable --now chronyd > /dev/null 2>&1
}

function securityConf(){
  sed -i '/^SELINUX=enforcing/s@enforcing@disabled@' /etc/selinux/config
  sed -i '/^SELINUX=permissive/s@permissive@disabled@' /etc/selinux/config
  setenforce 0 > /dev/null 2>&1
  systemctl stop firewalld.service
  systemctl disable firewalld.service > /dev/null 2>&1
  echo -e "${yellow}Warn:${plain} Firewalld is stop !"
}

function personConf(){

    swapoff -a
    sed -i 's/.*swap.*/#&/' /etc/fstab

    chmod +x /etc/rc.d/rc.local
    
    cat /etc/issue > /etc/issue.bak
    cat /dev/null > /etc/issue
    
    timedatectl set-timezone "Asia/Shanghai"
    
    rm -f  /usr/lib/systemd/system/ctrl-alt-del.target
    echo "set ts=4" >> /root/.vimrc
    echo "set expandtab" >> /root/.vimrc
    echo "set shiftwidth=4" >> /root/.vimrc
    echo "set autoindent" >> /root/.vimrc	
    echo "set paste" >> /root/.vimrc
}

function deal(){
  historyConf
  chronyConf
  securityConf
  personConf
}


function main(){
  action "$0" deal
}

main $*
